package com.one.common.location_service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.one.common.ILocationHelperServiceAIDL;
import com.one.common.ILocationServiceAIDL;
import com.one.common.receiver.NotificationHandle;

import androidx.annotation.Nullable;

/**
 * Author: zhulunjun
 * Email: zhulunjun@qq.com
 * CreateDate: 2019/5/24 16:03
 * Description：
 **/
public class NotificationService extends Service  {
    /**
     * startForeground的 noti_id
     */
    public static int NOTI_ID = 123321;

    private ServiceUtils.CloseServiceReceiver mCloseReceiver;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("background location", "远程服务调用成功");
        mCloseReceiver = new ServiceUtils.CloseServiceReceiver(this);
        registerReceiver(mCloseReceiver,ServiceUtils.getCloseServiceFilter());
        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        if (mCloseReceiver != null) {
            unregisterReceiver(mCloseReceiver);
            mCloseReceiver = null;
        }

        super.onDestroy();
    }


    private final String mHelperServiceName = "com.one.common.location_service.LocationHelperService";
    /**
     * 触发利用notification增加进程优先级
     */
    protected Notification applyNotiKeepMech() {
        Intent intentNotification = new Intent(Intent.ACTION_VIEW,
                Uri.parse("ycpcarapp://openpage?actionAndroid=com.ycp.car.main.ui.activity.MainActivity&param1=1"));
        intentNotification.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intentPend = PendingIntent.getActivity(this, 0,
                intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification notification = new NotificationHandle(this).crate("货有友持续为您服务",
                "点击返回应用", intentPend);
        startForeground(NOTI_ID, notification);
        startBindHelperService();
        return notification;
    }

    public void unApplyNotiKeepMech() {
        stopForeground(true);
    }

    public Binder mBinder;

    public class LocationServiceBinder extends ILocationServiceAIDL.Stub {

        @Override
        public void onFinishBind(){
        }

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }
    }

    private ILocationHelperServiceAIDL mHelperAIDL;

    private void startBindHelperService() {
        connection = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
                //doing nothing
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                ILocationHelperServiceAIDL l = ILocationHelperServiceAIDL.Stub.asInterface(service);
                mHelperAIDL = l;
                try {
                    l.onFinishBind(NOTI_ID);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        };
        Intent intent = new Intent();
        intent.setAction(mHelperServiceName);
        intent.setComponent(new ComponentName(getPackageName(),mHelperServiceName));
        bindService(ServiceUtils.getExplicitIntent(getApplicationContext(), intent), connection, Service.BIND_AUTO_CREATE);
    }

    private ServiceConnection connection;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        if (mBinder == null) {
            mBinder = new LocationServiceBinder();
        }
        return mBinder;
    }


}
